#include "DbPart.h"
#include "connectpool.h"
#include <sstream>
#include <vector>
#include "Game.h"
#include "Logic.h"
#include "RedisClient.h"
#include "util.h"
#include "def.h"


char g_szSQL[2056]={0};
#define MAX_SQL_BUF_LEN (sizeof(g_szSQL)-1)

CDbPart::CDbPart()
{
	m_dbHandler = NULL;
}

CDbPart::~CDbPart()
{
}


// 查询用户uid
bool CDbPart::GetUserId(const std::string& uuid, uint32& uid)
{
	int LoginId = 0;
	StrToInt(uuid, LoginId);
	memset(g_szSQL, 0, sizeof(g_szSQL));
	snprintf(g_szSQL, sizeof(g_szSQL)-1, "select `uid`,`uuid` from user_account_%d where uuid = '%s'", LoginId , uuid.c_str());
	log_debug("GetUserId-> SQL=%s", g_szSQL);
	if (m_dbHandler->m_pMysqlConn->GetConnect() == NULL)
	{
		m_dbHandler->m_strError = m_dbHandler->m_pMysqlConn->What();
		log_error("CallPro1 Error: %s\r Sql=%s \n", m_dbHandler->m_pMysqlConn->What().c_str(), g_szSQL);
		return false;
	}
	if (m_dbHandler->m_pStore->Query(g_szSQL))
	{
		unsigned long count = m_dbHandler->m_pStore->RowCount();
		if (count == 0)
		{
			log_error("User is not exist");
			uid = 0;
			return true;
		}
		uid = m_dbHandler->m_pStore->GetItemLongLong(0, "uid");
		return true;
	}
	else
	{
		log_error("Sql execution failed SQL=%s \n", g_szSQL);
	}
	return false;
}

bool CDbPart::GetUserByUid(std::uint32_t uid, std::string& uuid, std::string& agency, std::string& username, std::uint32_t& agency_id)
{
	memset(g_szSQL, 0, sizeof(g_szSQL));
	snprintf(g_szSQL, sizeof(g_szSQL)-1, "select `uid`,`uuid`,`agency`,`username`,`agency_id` from userid_%d where uid = %d", uid % TH_TABLE , uid);
	log_debug("GetUserByUid-> SQL=%s", g_szSQL);
	if (m_dbHandler->m_pMysqlConn->GetConnect() == NULL)
	{
		m_dbHandler->m_strError = m_dbHandler->m_pMysqlConn->What();
		log_error("CallPro1 Error: %s\r Sql=%s \n", m_dbHandler->m_pMysqlConn->What().c_str(), g_szSQL);
		return false;
	}
	if (m_dbHandler->m_pStore->Query(g_szSQL))
	{
		unsigned long count = m_dbHandler->m_pStore->RowCount();
		if (count == 0)
		{
			log_error("User is not exist");
			uuid = "";
			return false;
		}
		// uid = m_dbHandler->m_pStore->GetItemLongLong(0, "uid");
		uuid = m_dbHandler->m_pStore->GetItemString(0, "uuid");
		agency = m_dbHandler->m_pStore->GetItemString(0, "agency");
		username = m_dbHandler->m_pStore->GetItemString(0, "username");
		agency_id = m_dbHandler->m_pStore->GetItemLongLong(0, "agency_id");
		return true;
	}
	else
	{
		log_error("Sql execution failed SQL=%s \n", g_szSQL);
	}
	return false;
}

bool CDbPart::GetUserByUuid(const std::string& uuid, std::uint32_t& uid, std::string& agency, std::string& username, std::uint32_t& agency_id)
{
	int LoginId = 0;
	StrToInt(uuid, LoginId);
	memset(g_szSQL, 0, sizeof(g_szSQL));
	snprintf(g_szSQL, sizeof(g_szSQL)-1, "select `uid`,`uuid`,`agency`,`username`,`agency_id` from user_account_%d where uuid = '%s'", LoginId , uuid.c_str());
	log_debug("GetUserId-> SQL=%s", g_szSQL);
	if (m_dbHandler->m_pMysqlConn->GetConnect() == NULL)
	{
		m_dbHandler->m_strError = m_dbHandler->m_pMysqlConn->What();
		log_error("CallPro1 Error: %s\r Sql=%s \n", m_dbHandler->m_pMysqlConn->What().c_str(), g_szSQL);
		return false;
	}
	if (m_dbHandler->m_pStore->Query(g_szSQL))
	{
		unsigned long count = m_dbHandler->m_pStore->RowCount();
		if (count == 0)
		{
			log_error("User is not exist");
			uid = 0;
			return false;
		}
		// uuid = m_dbHandler->m_pStore->GetItemString(0, "uuid");
		uid = m_dbHandler->m_pStore->GetItemLongLong(0, "uid");
		agency = m_dbHandler->m_pStore->GetItemString(0, "agency");
		username = m_dbHandler->m_pStore->GetItemString(0, "username");
		agency_id = m_dbHandler->m_pStore->GetItemLongLong(0, "agency_id");
		return true;
	}
	else
	{
		log_error("Sql execution failed SQL=%s \n", g_szSQL);
	}
	return false;
}

// 注册用户基本信息
bool CDbPart::RegisterNewPlayer(const std::string& uuid, const std::uint32_t uid, const std::string& agency, const std::string& username, const std::uint32_t agency_id)
{
	int LoginId = 0;
	StrToInt(uuid, LoginId);
	do
	{
		// 创建新用户login
		std::vector<std::string> sqls;
		{
			char tmpSql[1024] = {0};
			snprintf(tmpSql, sizeof(tmpSql)-1, "INSERT INTO user_account_%d(uuid,uid,agency,username,agency_id) VALUES (\'%s\',%u,\'%s\',\'%s\',%d)", LoginId, uuid.c_str(), uid, agency.c_str(), username.c_str(), agency_id);
			log_debug("user_account-> SQL=%s", tmpSql);
			sqls.push_back(tmpSql);
		}
		{
			char tmpSql[1024] = {0};
			snprintf(tmpSql, sizeof(tmpSql)-1, 
				"INSERT INTO userid_%d(uuid,uid,agency,username,agency_id) VALUES (\'%s\',%u,\'%s\',\'%s\',%d)", 
				uid % TH_TABLE, uuid.c_str(), uid, agency.c_str(), username.c_str(), agency_id);
			log_debug("userid-> SQL=%s", tmpSql);
			sqls.push_back(tmpSql);
		}
		bool RetExc1 = m_dbHandler->UpdateSql(sqls);
		if (!RetExc1)
		{
			log_error("RetExc is falied RetExc2=%d,%s,%s",RetExc1,sqls.front().c_str(), sqls.back().c_str());
			break;
		}
		return true;
	} while(false);

	return false;
}
// 字符串转整数
bool CDbPart::StrToInt(const std::string& str, int& num)
{
	int value = 0;
	std::string::const_iterator it = str.begin();
	for (; it != str.end(); it++)
	{
		value += *it;
	}
	num = value % TH_TABLE;
    return true;
}

bool CDbPart::GetCurrency(stCurrency& st_currency)
{
	do
	{
		std::stringstream sql;
		sql << "select unit_multi, label from currency where currency='" << st_currency.currency << "'";
		bool ret = m_agencyDb->m_pStore->Query(sql.str().c_str());
		if (!ret)
		{
			log_error("ret is falied %d, %s",ret, sql.str().c_str());
			break;
		}
		log_debug("sql:%s", sql.str().c_str());
		unsigned long count = m_agencyDb->m_pStore->RowCount();
		if (count <= 0)
		{
			log_error("count is %lu, %s",count, sql.str().c_str());
			break;
		}
		st_currency.unit_multi = (m_agencyDb->m_pStore->GetItemLongLong(0, "unit_multi"));
		st_currency.label = (m_agencyDb->m_pStore->GetItemString(0, "label"));
		return true;
	} while(false);
	return false;
}


